<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewpoint" content="width=device,initial-scale=1">
    <link href="font-awesome-4.7.0/css/font-awesome.css" rel="stylesheet">
    <link href="reset.css" rel="stylesheet">
    <link href="icon.css" rel="stylesheet">
    <link href="register.css" rel="stylesheet">
    <title>饿了么 用户注册</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <style>
        .title, input {
            font-size: 3.4vw;
        }
        li {
            display: flex;
        }
    </style>
</head>

<body>
<div class="wrapper">
    <!--  header部分-->
    <header>
        <p>用户注册</p>
    </header>
    <!--    表单部分-->
    <ul class="form-box">
        <li>
            <div class="title">
                手机号码：
            </div>
            <div class="content">
                <input style="font-size: 3.4vw;" type="text" placeholder="手机号码" id="userId">
            </div>
        </li>
        <li>
            <div class="title">
                姓名：
            </div>
            <div class="content">
                <input style="font-size: 3.4vw;" type="text" placeholder="姓名" id="userName">
            </div>
        </li>
        <li>
            <div class="title">
                密码：
            </div>
            <div class="content">
                <input style="font-size: 3.4vw;" type="password" placeholder="密码" id="password">
            </div>
        </li>
        <li>
            <div class="title">
                确认密码：
            </div>
            <div class="content">
                <input style="font-size: 3.4vw;" type="password" placeholder="确认密码" id="confirmPass">
            </div>
        </li>
        <li>
            <div class="title">
                性别：
            </div>
            <div class="content" style="font-size: 3.4vw">
                <input type="radio" name="userSex" value="1" checked style="width:6vw;height: 3.2vw">男
                <input type="radio" name="userSex" value="2" style="width:6vw;height: 3.2vw">女
            </div>
        </li>
        <li>
            <div class="title">
                地址：
            </div>
            <div class="content">
                <input style="font-size: 3.4vw;" type="text" placeholder="地址" id="address">
            </div>
        </li>
    </ul>

    <div class="button-login">
        <button id="registerButton">注册</button>
    </div>
    <p id="message" style="font-size: 14px; margin-top: 20px;"></p> <!-- 用于显示注册结果的消息 -->

    <!--    底部菜单部分-->
    <ul class="footer">
        <li onclick="location.href='index.html' ">
            <i class="fa fa-home"></i>
            <p>首页</p>
        </li>
        <li onclick="location.href='Message.html'">
            <i class="fa fa-compass"></i>
            <p>消息</p>
        </li>
        <li onclick="location.href='orderList.html' ">
            <i class="fa fa-file-text-o"></i>
            <p>订单</p>
        </li>
        <li>
            <i class="fa fa-user-o"></i>
            <p>我的</p>
        </li>
    </ul>
</div>
</body>
<script>
    $(document).ready(function () {
        $('#registerButton').click(function () {
            var userId = $('#userId').val();
            var userName = $('#userName').val();
            var password = $('#password').val();
            var confirmPass = $('#confirmPass').val();
            var userSex = parseInt($('input[name="userSex"]:checked').val());
            var address = $('#address').val();

            // 简单的验证逻辑
            if (password !== confirmPass) {
                alert('密码和确认密码不匹配！')
                return;
            }

            if (userId == '') {
                alert('手机号不能为空');
                return;
            }
            if (userName == '') {
                alert('姓名不能为空');
                return;
            }

            if (password == '') {
                alert('密码不能为空');
                return;
            }

            if (address == '') {
                alert('地址不能为空');
                return;
            }

            // 假设我们有一个后端 API 来处理注册请求
            // 这里我们用一个简单的模拟来展示
            $.ajax({
                url: 'http://127.0.0.1:8081/user/register', // 替换为实际的注册 API URL
                type: 'POST',
                contentType: 'application/json; charset=UTF-8', // 设置内容类型为 JSON
                data: JSON.stringify({
                    userId: userId,
                    password: password,
                    userSex: userSex,
                    userName: userName,
                    address: address
                }),
                success: function (res) {
                    if (res.flag) {
                        window.location.href = 'login.html';
                    } else {
                        alert(res.message)
                    }
                },
                error: function (xhr, status, error) {
                    alert('注册失败：' + xhr.statusText);
                }
            });
        });
    });
</script>

</html>